package com.hotline.communication.service.impl;


import com.hotline.communication.constant.Constants;
import com.hotline.communication.mapper.DutyUserCallMapper;
import com.hotline.communication.mapper.DutyUserMapper;
import com.hotline.communication.dto.DutyUserCallDTO;
import com.hotline.communication.dto.DutyUserDTO;
import com.hotline.communication.service.DutyUserService;
import com.hotline.communication.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;


@Service
@Slf4j
public class DutyUserServiceImpl implements DutyUserService {

    @Resource
    private DutyUserMapper dutyUserMapper;

    @Resource
    private DutyUserCallMapper dutyUserCallMapper;

    @Override
    public DutyUserDTO selectById(String id) {
        log.info("ACCESS_LOG[]duty_user[]值班人员[]根据ID查询值班人员[]{}",id);
        DutyUserDTO dutyUserDTO = dutyUserMapper.selectById(id);
        return dutyUserDTO;
    }

    @Override
    public DutyUserDTO selectByCallId(String callId) {
        log.info("ACCESS_LOG[]duty_user[]值班人员[]根据呼叫流水号查询值班人员[]{}",callId);
        DutyUserDTO dutyUserDTO = dutyUserMapper.selectByCallId(callId);
        return dutyUserDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized DutyUserDTO getDutyUser(String unitCode,String callId,Integer isLock) {
        log.info("ACCESS_LOG[]duty_user[]值班人员[]取值班人员信息[]");
        try{
            DutyUserDTO dutyUserDTO = dutyUserMapper.getDutyUser(unitCode,isLock);
            //获取值班人员后锁定
            if(Objects.nonNull(dutyUserDTO)){
                DutyUserDTO dutyUser = new DutyUserDTO();
                dutyUser.setPhone(dutyUserDTO.getPhone());
                dutyUser.setIsLock(Constants.YES);
                dutyUser.setCallTime(new Date());
                dutyUser.setCallId(callId);

                dutyUserMapper.update(dutyUser);

                DutyUserCallDTO dutyUserCallDTO = new DutyUserCallDTO();
                BeanUtils.copyProperties(dutyUserDTO,dutyUserCallDTO);
                String id = UUIDUtils.getUUID();
                dutyUserCallDTO.setId(id);
                dutyUserCallDTO.setCallId(callId);
                dutyUserCallMapper.insert(dutyUserCallDTO);
            }
            return dutyUserDTO;
        }catch (Exception e){
            log.error("取值班人员信息失败, unitCode={},callId={}， cause:{}", unitCode,callId,e.getMessage());
        }
        return null;
    }

    @Override
    public synchronized void update(DutyUserDTO dutyUserDTO) {
        try {
            log.info("ACCESS_LOG[]duty_user[]值班人员[]更新值班人员信息[]{}",dutyUserDTO);
            dutyUserMapper.update(dutyUserDTO);
        }catch (Exception e){
            log.error("更新值班人员信息失败, dutyUserDTO={}, cause:{}", dutyUserDTO,e.getMessage());
        }
    }

    @Override
    public synchronized void updateByCallId(DutyUserDTO dutyUserDTO) {
        try {
            log.info("ACCESS_LOG[]duty_user[]值班人员[]根据呼叫流水号更新值班人员信息[]{}",dutyUserDTO);
            dutyUserMapper.updateByCallId(dutyUserDTO);
        }catch (Exception e){
            log.error("更新值班人员信息失败, dutyUserDTO={}, cause:{}", dutyUserDTO,e.getMessage());
        }
    }
}
